package com.base.system.managedBean;

import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.user.util.Browser;

import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.entity.CommonKeyType;
import com.base.commonKey.service.ICommonKeyService;
import com.base.commons.SpringBeanHolder;
import com.base.security.SecurityConst;
import com.base.system.managedBean.baseBean.BaseCommonKeyBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_editCommonKeyBean", scope = ManagedBeanScope.REQUEST)
public class EditCommonKeyBean extends BaseCommonKeyBean {
	Log logger = LogFactory.getLog(EditCommonKeyBean.class);

	ICommonKeyService commonKeyService = SpringBeanHolder
			.getBean("commonKeyService");

	@Inject
	FacesContext context;

	@Accessible
	private String freeItemStyle = "display:none";;

	@Accessible
	private String parentIdStyle = "display:none";

	@Accessible
	private String keyType;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();
			commonKey = (CommonKey) session
					.get(SecurityConst.COMMONKEY_SELECT_ROW_DATA);

			if (commonKey.getKeyType().equals("showType")) {
				freeItemStyle = "display:block";
			}
			keyType = commonKey.getKeyType();
			CommonKeyType ckType = commonKeyService
					.getCommonKeyTypeByTypeCode(keyType);

			if (ckType != null && ckType.getIsLay()) {
				parentIdStyle = "display:block";
			}
			this.getCommonKeyProperty();
		}
	}

	@Action(id = "saveCommonKey")
	public void saveCommonKey() {
		// commonKey = new CommonKey();

		// Map<String, Object> session = context.getExternalContext()
		// .getSessionMap();
		// commonKey = (CommonKey) session
		// .get(SecurityConst.COMMONKEY_SELECT_ROW_DATA);

		commonKey = commonKeyDao.getById(Integer.parseInt(id));
		if (id != null && !"".equals(id))
			commonKey.setId(Integer.parseInt(id));
		setCommonKeyProperty();
		commonKeyDao.saveOrUpdate(commonKey);
		String script = "closeCenterLayoutTab('"
				+ SecurityConst.COMMONKEY_EDIT_TAB + "')";
		Browser.execClientScript("window.top", script);
	}
}
